package umbook.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import umbook.domain.Comentarios;
import umbook.domain.Muro;
import umbook.domain.Usuario;
import umbook.service.ServiceComentarios;
import umbook.service.ServiceUsuario;

@RequestMapping("/comentarioses/**")
@Controller
@RooWebScaffold(path = "comentarioses", formBackingObject = Comentarios.class)
public class ComentariosController {
    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping
    public String index() {
        return "comentarioses/index";
    }
	
	
	
	@SuppressWarnings("static-access")
	@RequestMapping(value = "/comentarios", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<Comentarios> comentarios (@RequestParam(value="comentario",required=false) String comentario,
    		@RequestParam(value="idu",required=false) String idu,
    		@RequestParam(value="idm",required=false) String idm,
    		ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, Model uiModel){
	    	HttpHeaders headers = new HttpHeaders();
	        headers.add("Content-Type", "application/json; charset=utf-8");
    		Comentarios c=new Comentarios();
    		Muro muro =ServiceComentarios.findMuro(idm);
    		Usuario usuario = ServiceComentarios.findUsuario(idu);
    		List<Comentarios>comentarios2 = ServiceComentarios.findComentarios(usuario);
    		c.setComentario(comentario);
    		c.setUsuario(usuario);
    		c.setMuro(muro);
    		ServiceComentarios.addComentario(c);
			return comentarios2;
    		
        
    }
    @RequestMapping(value = "/deleteComent", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<Comentarios> deleteComent(@RequestParam(value="idComentario",required=false) Long idComentario,
    		ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, Model uiModel){
				ServiceUsuario.deleteComent(Comentarios.findComentarios(idComentario));
				List<Comentarios> c = new Comentarios().findAllComentarioses();
    		return c;
    	
    }
    @RequestMapping(value = "/listarComentarios", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<Comentarios> listarComentarios(@RequestParam(value="idu",required=false) Long idUsuario,
    		@RequestParam(value="idm",required=false) Long idMuro,
    		ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, Model uiModel){
			List<Comentarios> c = ServiceComentarios.listarComentarios(idUsuario,idMuro);	
    		return c;
    	
    }
}
